package com.wz.rfc;

import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;

import java.util.HashMap;
import java.util.Properties;

public class CustomSAPDestinationDataProvider {
    private static final String ABAP_AS_POOLED = "ABAP_AS2";
    static class MyDestinationDataProvider implements DestinationDataProvider {

        private DestinationDataEventListener eL;


        private HashMap<String, Properties> destinations;


        private static MyDestinationDataProvider provider = new MyDestinationDataProvider();


        private MyDestinationDataProvider() {// 单例模式

            if (provider == null) {

                destinations = new HashMap<String, Properties>();

            }

        }


        public static MyDestinationDataProvider getInstance() {

            return provider;

        }


        // 实现接口：获取连接配置属性

        public Properties getDestinationProperties(String destinationName) {

            if (destinations.containsKey(destinationName)) {

                return destinations.get(destinationName);

            } else {

                throw new RuntimeException("Destination " + destinationName

                        + " is not available");

            }

        }


        public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {

            this.eL = eventListener;

        }


        public boolean supportsEvents() {

            return true;

        }


        /**
         * Add new destination 添加连接配置属性
         *
         * @param properties holds all the required data for a destination
         **/

        void addDestination(String destinationName, Properties properties) {

            synchronized (destinations) {

                destinations.put(destinationName, properties);

            }

        }

    }

    public static void main(String[] args) throws Exception {


        // 获取单例

        MyDestinationDataProvider myProvider = MyDestinationDataProvider.getInstance();


        // Register the MyDestinationDataProvider 环境注册

        Environment.registerDestinationDataProvider(myProvider);


        // TEST 02：连接池测试

        // Add another destination to test

        // ABAP_AS2 is the test destination name

        String destinationName2 = "ABAP_AS2";

        System.out.println("Test destination - " + destinationName2);

        Properties connectProperties2 = new Properties();

        connectProperties2.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.3.169");//服务器
        connectProperties2.setProperty(DestinationDataProvider.JCO_SYSNR, "00");        //系统编号
        connectProperties2.setProperty(DestinationDataProvider.JCO_CLIENT, "140");       //SAP集团
        connectProperties2.setProperty(DestinationDataProvider.JCO_USER, "RFCUSER_KQ");  //SAP用户名
        connectProperties2.setProperty(DestinationDataProvider.JCO_PASSWD, "mwsap2010");     //密码
        connectProperties2.setProperty(DestinationDataProvider.JCO_LANG, "zh");        //登录语言
        connectProperties2.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");  //最大连接数
        connectProperties2.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");     //最大连接线程

        myProvider.addDestination(destinationName2, connectProperties2);


        // Get a destination with the name of "ABAP_AS2"

        JCoDestination DES_ABAP_AS2 = JCoDestinationManager.getDestination(destinationName2);
        // Test the destination with the name of "ABAP_AS2"

        try {

            DES_ABAP_AS2.ping();
            System.out.println("Destination - " + destinationName2 + " is ok");

            accessSAPStructure();
        } catch (Exception ex) {

            ex.printStackTrace();

            System.out.println("Destination - " + destinationName2

                    + " is invalid");

        }

    }

    public static void accessSAPStructure() throws JCoException {

        JCoDestination destination = JCoDestinationManager

                .getDestination(ABAP_AS_POOLED);

        JCoFunction function = destination.getRepository().getFunction(

                "ZFL_WMS_GET_MATERIAL");//从对象仓库中获取 RFM 函数

        if (function == null)

            throw new RuntimeException(

                    "RFC_SYSTEM_INFO not found in SAP.");


        try {

            function.execute(destination);

        } catch (AbapException e) {

            System.out.println(e.toString());

            return;

        }


        JCoStructure exportStructure = function.getExportParameterList()

                .getStructure("EV_TYPE");

        System.out.println("System info for "

                + destination.getAttributes().getSystemID() + ":\n");

        for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {

            System.out.println(exportStructure.getMetaData().getName(i) + ":\t"

                    + exportStructure.getString(i));

        }

        System.out.println();


        // JCo still supports the JCoFields, but direct access via getXX is more

        // efficient as field iterator  也可以使用下面的方式来遍历

        System.out.println("The same using field iterator: \nSystem info for "

                + destination.getAttributes().getSystemID() + ":\n");

        for (JCoField field : exportStructure) {

            System.out.println(field.getName() + ":\t" + field.getString());

        }

        System.out.println();


        //*********也可直接通过结构中的字段名或字段所在的索引位置来读取某个字段的值

        System.out.println("RFCPROTO:\t" + exportStructure.getString(0));

        System.out.println("RFCPROTO:\t" + exportStructure.getString("RFCPROTO"));

    }

}


